adminPanel.controller('StudentRecommendationStrengthGrid',['$scope', '$filter', '$http', '$q','cacheFactory', 
                                                      function($scope, $filter, $http, $q, cacheFactory) {

 
	$q;
	$scope.gradeList = cacheFactory.getGradeList();
	$scope.sportsList = cacheFactory.getSportsList();
	$scope.medalsList = cacheFactory.getMedalsList();
	$scope.competitionLevelList = cacheFactory.getCompetitionLevelList();
	
	
	$scope.showGrid=false;
	
	
	//this is earch criteria for sports row
	$scope.searchCriteria={studentEntityId:1, termId:1, gradeId:1};
	
	//use to send data to server
	var studentRecord={"sportsActivities":null, "searchCriteria":null};
	
	//get the Sports loaded into system
	$scope.sportsVos =[];
	
	$scope.getTestDetails=function(){
		$scope.sportsTestVos = [
		       	    {cid: 1, sid: 1, sportId: 1, dated:'2014-05-14T18:30:00.000Z', ranking: 90, medal: 'Gold', remarks: 'he is talented',sportsAttachmentId:'TBD',newRow:false},
		       	    {cid: 2, sid: 2, sportId: 2, dated:'2014-05-14T18:30:00.000Z', ranking: 90, medal: 'Silver', remarks: 'Rocking',sportsAttachmentId:'TBD',newRow:false},
		       	    {cid: 3, sid: 3, sportId: 3, dated:'2014-05-14T18:30:00.000Z', ranking: 90, medal: 'Bronze', remarks: 'Can do better in Basketball',sportsAttachmentId:'TBD',newRow:false},
		       	  ];
		$scope.showGrid=true;
	}
	
	
	
	
	$scope.getDetails= function(){                 
		studentRecord.searchCriteria= $scope.searchCriteria;
		$http.post(BASE_URL+"admin/handleRequest/studentRecord/GET_SPORTS_BY_GRADE/",studentRecord).success(function(data){
			if(data.status=='SUCCESS'){
				$scope.sportsVos=data.result;	
				$scope.showGrid=true;
			}else{
				alert(data.status +":"+data.msg);
				$scope.showGrid=true;
				
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	}
	
	$scope.criteriaChanged=function(){
		$scope.showGrid=false;
		$scope.sportsVos=[];
	}

	  $scope.checkValue = function(data, id) {
		  //validation will go here
	  };
	  
	  $scope.showSports = function(sportsVo) {
		    var selected = [];
		    if(sportsVo.sportId) {
		      selected = $filter('filter')($scope.sportsList, {id: sportsVo.sportId});
		    }
		    return selected.length ? selected[0].sportName : 'Not set';
		};
		
		$scope.showSportsCompetitionLevel = function(sportsVo) {
		    var selected = [];
		    if(sportsVo.competitionLevel) {
		      selected = $filter('filter')($scope.competitionLevelList, {id: sportsVo.competitionLevel});
		    }
		    return selected.length ? selected[0].value : 'Not set';
		};
		

	  // filter sportsVo to show
	  $scope.filterSports = function(sportsRow) {
	    return sportsRow.rowDeleted !== true;
	  };

	  // mark user as deleted
	  $scope.deleteSportsRow = function(cid,sid) {
		  //alert(cid);
	    var filtered = $filter('filter')($scope.sportsVos, {cid: cid});
	    if (filtered.length) {
	      filtered[0].rowDeleted = true;
	      $scope.sportsVos[cid].rowDeleted=true;
	    }
	  };

	  // add user
	  $scope.addSportsRow = function() {
	    $scope.sportsVos.push({
	      cid: $scope.sportsVos.length+1,
	      sid: 0,
	      sportId: null,
	      dated: new Date(),
	      gradeId:$scope.searchCriteria.gradeId,
	      ranking: null,
	      competitionLevel:null,
	      medal: null,
	      remarks:null,
	      sportsAttachmentId: null,
	      newRow:true,
	    });
	  };

	  // cancel all changes
	  $scope.cancel = function() {
	    for (var i = $scope.sportsVos.length; i--;) {
	      var vo = $scope.sportsVos[i];    
	      // undelete
	      if (vo.rowDeleted) {
	    	$scope.sportsVos[i].rowDeleted = false;
	        delete vo.isDeleted;
	      }
	      // remove new 
	      if (vo.newRow) {
	        $scope.sportsVos.splice(i, 1);
	      }
	    };
	  };

	  // save edits
	  $scope.saveTable = function() {
	    var results = [];
	    for (var i = $scope.sportsVos.length; i--;) {
	      var vo = $scope.sportsVos[i];
	      // actually delete user
	      if (vo.rowDeleted) {
	        //$scope.sportsVo.splice(i, 1);
	      }
	      // mark as not new 
	      if (vo.newRow) {
	    	  vo.newRow = true;
	      }
	      vo.gradeId=$scope.searchCriteria.gradeId;
	    }
	    //Create student vo object
	    studentRecord.sportsActivities=$scope.sportsVos;
	    studentRecord.searchCriteria= $scope.searchCriteria;
	    // send on server
	    var httpResult=$http.post(BASE_URL+'admin/handleRequest/studentRecord/SAVE_SPORTS', studentRecord);
	    httpResult.success(function(data){
	    		alert(data.status);
	    });
	    results.push(httpResult);
	    return $q.all(results);
	  };
	
	
}]);